﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bitirme.Controllers;
using Bitirme.DAL.Entities;

namespace Bitirme.Views
{
    public partial class ShowProjects : System.Web.UI.Page
    {
        ProjectHandler projectHandler;
        WorkPackageHandler workPackageHandler;

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {



            if (e.CommandName == "show_gantt" || e.CommandName == "show_assignment" || e.CommandName == "workpackage" || e.CommandName == "assign" || e.CommandName == "update" || e.CommandName == "deleteProject" || e.CommandName == "show_workpackages")
            {
                int rowNum = int.Parse(e.CommandArgument.ToString()); 
                string choice = e.CommandName;
                string iD = GridView1.Rows[rowNum].Cells[0].Text;


                if (choice == "deleteProject") {

                    projectHandler = new ProjectHandler();
                    workPackageHandler = new WorkPackageHandler();

                    if (workPackageHandler.GetAllWorkPackagesByProjectID(int.Parse(iD)).Count > 0)
                        Label1.Text = "Önce Projeye Eklenen İş Paketlerini Silin!";
                    else
                    {
                        projectHandler.DeleteProjectByID(int.Parse(iD)); // cascade olunca diger kayıtlar silinecek diye öbür tablolari güncellemedim!!!
                        Response.Redirect("ShowProjects.aspx");
                    }
                }

                else if (choice == "show_workpackages")
                {
                    Response.Redirect("ShowWorkPackages.aspx?id=" + iD);
                }

                else if (choice == "workpackage")
                    Response.Redirect("AddWorkPackage.aspx?id=" + iD);
                else if (choice == "assign")
                {
                    int projectID = Int32.Parse(iD);
                    AssignmentHandler asHandler = new AssignmentHandler();

                    ProjectHandler prHandler = new ProjectHandler();
                    Project project = prHandler.GetProjectsWithAllAttributesByID(projectID);
                    List<Project> allProjects = new List<Project>();

                    List<Project> projects = prHandler.GetAllProjects();

                    foreach (Project projectItem in projects)
                    {

                        project = prHandler.GetProjectsWithAllAttributesByID(projectItem.ID);
                        allProjects.Add(project);
                    }

                    ScheduleInterProjects schedulerAll = new ScheduleInterProjects(allProjects);
                    List<Assignment> assignedList = schedulerAll.assignWorkPackages();
                    if (schedulerAll.Result != String.Empty) { 
                    
                        Response.Write( "<script>alert('"+schedulerAll+"')</script>");
                    }
                    asHandler.InsertAssignments(assignedList);
                    Response.Redirect("ShowProjects.aspx");
                }
                else if (choice == "update")
                    Response.Redirect("UpdateProject.aspx?id=" + iD);
                else if (choice == "show_assignment")
                    Response.Redirect("ShowAssignment.aspx?id=" + iD);
                else if (choice == "show_gantt")
                    Response.Redirect("GanttChart.aspx?id=" + iD);

            }
        }

    }
}